<?php

require_once("login/src/Model/UserInfo.php");
require_once("login/src/Model/LoginValidation.php");
//require_once("Database/Model/DatabaseConnection.php");
require_once("Database/Model/WorkoutExercisesDAL.php");


class MyWorkoutDAL
{

     /**
    *@var LoginValidation 
    **/
	private $loginValidation;

    /**
    *@var WorkoutExercisesDAL 
    **/
	private $workoutExercisesDAL;

	public function __construct()
	{
        $this->loginValidation = new LoginValidation();
		$this->workoutExercisesDAL = new WorkoutExercisesDAL();
	}


    /**
    *@param int 
    **/
    public function DeleteWorkout($workoutID)
    {
        $sql = "DELETE FROM myworkouts WHERE `WorkoutID` = $workoutID";  

        $mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $mysqli->close();
        $this->workoutExercisesDAL->DeleteWorkoutExercise($workoutID);
    }

    /**
    *@param int
    *@return workout / array() workout 
    **/
	public function getWorkouts($workoutID = null)
	{
        if($workoutID != null)
        { 
            $sql = "SELECT WorkoutID, Name, Description, UserInfoID
                    FROM myworkouts
                    WHERE WorkoutID = $workoutID";
            $workout = $this->Workouts($sql);     
            if(isset($workout[0]))
            {
                return $workout[0];
            }
            else
            {
                return;
            }
        }
        else
        {
            $sql = "SELECT `WorkoutID`, Name, Description, UserInfoID FROM myworkouts";
		    return $this->Workouts($sql);
	    }
    }

     /**
    *@param string
    *@return array() workout 
    **/
	private function Workouts($sql)
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);


		$stmt = $mysqli->prepare($sql);
                if ($stmt == FALSE)
                {
                //        throw new \Exception("prepare of [$sql] failed " . $this->mysqli->error);
                }
                
                $result = $stmt->execute();
                if ($result == FALSE) 
                {
                        //throw new \Exception("execute of [$sql] failed " . $stmt->error);
                }

                $result = $stmt->bind_result($workoutID, $name, $description, $userInfoID);
                if ($result == FALSE) 
                {
                      //  throw new \Exception("execute of [$sql] failed " . $stmt->error);
                }

                $workoutList = array();

	            while ($stmt->fetch()) 
                {
	                $workoutList[] = MyWorkout::createWorkoutFromDatabase($workoutID, $name, $description, $userInfoID);
	            }
	            $mysqli->close();
                return $workoutList;
	}

     /**
    *@param MyWorkout
    *@param array() exercises
    *@return workout / array() workout 
    **/
	public function saveNewWorkout(MyWorkout $myWorkout, $exercises) 
	{
								//localhost ist för 127.0.0.1
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

		$sql = "INSERT INTO myworkouts 
                        (
                                Name, 
                                Description, 
                                UserInfoID
                        ) 
                        VALUES(?, ?, ?)";

            //http://www.php.net/manual/en/mysqli-stmt.prepare.php
            $statement = $mysqli->prepare($sql);
            if ($statement === FALSE) 
            {
                    // throw new \Exception("prepare of $sql failed " . $this->mysqli->error);
            }

		    $name = $myWorkout->getName();
            $description = $myWorkout->getDescription(); 
            $userID = $this->loginValidation->getLoggedInUserID();					

            if ($statement->bind_param("ssi", $name, 
                                               $description, 
                                               $userID) === FALSE) 
            {
                    //throw new \Exception("bind_param of $sql failed " . $statement->error);
            }

            if ($statement->execute() === FALSE) 
            {
                    // throw new \Exception("execute of $sql failed " . $statement->error);
            }
        $workoutID = $mysqli->insert_id;
		$mysqli->close();


		foreach($exercises as $ex)
		{	// add exercise id, and the workoutID from which we just created and create "workoutexercises"
			$this->workoutExercisesDAL->saveNewWorkoutExercise($workoutID, $ex);
		}
	}

}






